package com.scholarship.modules.backend.controller;

import com.scholarship.common.entity.RetCode;
import com.scholarship.common.entity.RetResponse;
import com.scholarship.common.entity.RetResult;
import com.scholarship.common.utils.NewFileUtil;
import com.scholarship.modules.admin.service.InOutFileService;
import com.scholarship.modules.backend.dao.RewardTableQuery;
import com.scholarship.modules.backend.dao.RewardTableSearch;
import com.scholarship.modules.backend.entity.StudentScore;
import com.scholarship.modules.backend.service.RewardTableService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.openxml4j.exceptions.OLE2NotOfficeXmlFileException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author zhz
 * @date 2021/5/31 20:41
 * 备注：奖学金表对应接口
 */
@Slf4j
@RestController
@Api(tags = {"奖学金表对应接口"})
public class RewardTableController {
    //日志logger
    private static final Logger logger = LoggerFactory.getLogger(RewardTableController.class);

    @Autowired
    RewardTableService rewardTableService;

    @Autowired
    InOutFileService inOutFileService;

    @Autowired
    NewFileUtil newFileUtil;

    @ApiOperation("根据tableId列出表头信息 @author zhz")
    @GetMapping("/backend/listTitleByTableId")
    public RetResult listTitleByTableId(Integer id){
        return RetResponse.makeRsp(RetCode.SUCCESS,"success",rewardTableService.listTitleByTableId(id));
    }

    @ApiOperation("分页实现根据查询条件，列出表的所有信息 @author zhz")
    @GetMapping("/backend/listRewardTableByQuery")
    public RetResult listRewardTableByQuery(RewardTableQuery rewardTableQuery){
        return RetResponse.makeRsp(RetCode.SUCCESS,"success",rewardTableService.listRewardTableByQuery(rewardTableQuery));
    }

    @ApiOperation("分页实现根据tableId列出表的所有信息 @author zhz")
    @GetMapping("/backend/listTableByTableId")
    public RetResult listTableByTableId(@RequestParam("id") Integer id,
                                        @RequestParam(value = "page",defaultValue = "1") Integer page,
                                        @RequestParam(value = "size",defaultValue = "10") Integer size){
        return RetResponse.makeRsp(RetCode.SUCCESS,"success",rewardTableService.listRewardTableByTableId(id,page,size));
    }

    @ApiOperation("列出所有表id和对应的表命 @author zhz")
    @GetMapping("/backend/listRewardTable")
    public RetResult listRewardTable(){
        return RetResponse.makeRsp(RetCode.SUCCESS,"success",rewardTableService.listRewardTable());
    }

    @ApiOperation("给学生批改成绩 @author zhz")
    @PostMapping("/backend/updateTableScore")
    public RetResult updateTableScore(@RequestBody StudentScore studentScore){
        return RetResponse.makeRsp(RetCode.SUCCESS,"success",rewardTableService.updateTableScore(studentScore));
    }

    @ApiOperation("更新学生的分数 @author zhz")
    @GetMapping("/backend/updateStuScore")
    public RetResult updateStuScore(){
        return RetResponse.makeRsp(RetCode.SUCCESS,"更新所有学生的成绩",rewardTableService.upStuScore());
    }

    @ApiOperation("根据表id列出表具有的属性名和中文名 @author zhz")
    @GetMapping("/backend/listTableAttribute")
    public RetResult listTableAttribute(Integer id){
        return RetResponse.makeRsp(RetCode.SUCCESS,"成功展示，格式为<中文名：属性名>",rewardTableService.listTableAttribute(id));
    }

    @ApiOperation("根据表id导出对应表格 @author zhz")
    @GetMapping("/backend/exportRewardTable")
    public ResponseEntity<byte[]> exportRewardTable(Integer id) throws Exception {
        return rewardTableService.exportRewardTable(id);
    }

    @ApiOperation("驳回学生奖学金申请 @author zhz")
    @PostMapping("/backend/giveStudentFeedback")
    public RetResult giveStudentFeedback(String userId,Integer tableId,String feedback){
        try {
            rewardTableService.giveStudentFeedback(userId,tableId,feedback);
            return RetResponse.makeOKRsp();
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"服务器内部错误");
        }
    }

    @ApiOperation("批改单个学生奖学金申请 @author zhz")
    @PostMapping("/backend/giveStudentScore")
    public RetResult giveStudentScore(String userId,Integer tableId,Float score){
        try {
            rewardTableService.giveStudentScore(userId,tableId,score);
            return RetResponse.makeOKRsp();
        }catch (Exception e){
            e.printStackTrace();
            logger.error(e.getMessage());
            return RetResponse.makeRsp(RetCode.INTERNAL_SERVER_ERROR,"服务器内部错误");
        }
    }

    @ApiOperation("导出学生上传的资料压缩包 @author zhz")
    @GetMapping("/backend/exportRewardPicturesZip")
    public void exportRewardPicturesZip(Integer id, HttpServletResponse response) throws Exception {
        rewardTableService.exportRewardPicturesZip(id,response);
    }

    @ApiOperation("分页实现根据关键词，模糊搜索出学生的奖学金表 @author zhz")
    @GetMapping("/backend/listRewardTableBySearch")
    public RetResult listRewardTableBySearch(RewardTableSearch rewardTableSearch){
        return RetResponse.makeRsp(RetCode.SUCCESS,"success",rewardTableService.listRewardTableBySearch(rewardTableSearch));
    }
}
